package lc.com.lcpicturebackend.manager.app;

import jakarta.annotation.Resource;
import lc.com.lcpicturebackend.manager.app.adviser.MyLoggerAdvisor;
import lc.com.lcpicturebackend.manager.app.chatmemory.MysqlMemoryChatMemory;
import lc.com.lcpicturebackend.manager.app.rag.AppRagCustomAdvisorFactory;
import lc.com.lcpicturebackend.model.entity.User;
import lc.com.lcpicturebackend.service.ConversationMsgService;
import lc.com.lcpicturebackend.service.ConversationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * 功能：摄影小助手客户端
 * 作者：lc
 * 日期：2025/5/25 16:19
 */
@Component
@Slf4j
public class PhotoChatClient {

    private final ChatClient dashboardChatClient;

    @Resource
    private ConversationService conversationService;

    @Resource
    private ConversationMsgService conversationMsgService;

    @Resource
    private VectorStore photoAppVectorStore;

    public PhotoChatClient(ChatClient.Builder builder) {
        InMemoryChatMemory inMemoryChatMemory = new InMemoryChatMemory();
        this.dashboardChatClient = builder.defaultSystem(ChatPromptManager.DEFAULT_SYSTEM_PROMPT)
                .defaultAdvisors(
                        // 自定义日志 Advisor，可按需开启
                        new MyLoggerAdvisor())
                .build();
    }

    /**
     * 发起聊天
     *
     * @param prompt 用户提示词
     * @param chatId 会话ID
     * @return
     */
    public String doChat(String prompt, Long chatId) {
        return this.dashboardChatClient.prompt()
                .advisors(advisor -> advisor.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5))
                .user(prompt)
                .call()
                .content();
    }

    public Flux<String> doChatDemo(String prompt, String chatId, User loginUser) {
        MysqlMemoryChatMemory mysqlMemoryChatMemory = new MysqlMemoryChatMemory(conversationService, conversationMsgService);
        return this.dashboardChatClient.prompt()
                .advisors(advisor -> advisor.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5)
                        .param("chat_memory_user", loginUser))
                .advisors(new MessageChatMemoryAdvisor(mysqlMemoryChatMemory),
                       new AppRagCustomAdvisorFactory().create(photoAppVectorStore, "构图"))
                .user(prompt)
                .stream()
                .content();
    }
}
